//: src/com/silvtar/sync/util/Bin.java
/* 
 * Created by 朴晟宁 <piaosn@gmail.com> on 5/24/2013. 
 * Copyright (c) 2013 朴晟宁. All rights reserved.
 * 
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program;
 * 
 * *********************************************************************
 * 
 * Modify History:
 * Date        Version    Description
 * 5/24/2013   0.1        Create the class
 */

package com.silvtar.util;

import java.util.ArrayList;

/**
 * 本类用于对执行一些常用命令进行包装。
 * 
 * @author 朴晟宁/Silvtar
 * @author piaosn@gmail.com
 * @version 0.1
 */
public class Bin {

    /**
     * 执行拷贝命令，只支持文件
     * 
     * @param source
     *            源文件
     * @param aim
     *            目标文件
     */
    public static void cp(String source, String aim) {
        RunCMD cmd = new RunCMD("./res/bin/cp " + source + " " + aim);
        cmd.printResult();
        cmd.printErrResult();
    }

    /**
     * 执行删除命令，只支持文件
     * 
     * @param fileName
     *            要删除的文件名
     */
    public static void rm(String fileName) {
        RunCMD cmd = new RunCMD("./res/bin/rm " + fileName);
        cmd.printResult();
        cmd.printErrResult();
    }

    /**
     * 开启rsyncd
     */
    public static void rsyncd(String configPath) {
        //RunCMD cmd = new RunCMD("./assets/rsync/rsync --daemon --config=" + configPath);
        RunCMD cmd = new RunCMD("rsync --daemon --config="
                + configPath);
        cmd.printResult();
        cmd.printErrResult();
    }

    /**
     * 运行rsync
     * 
     * @param cmds
     *            命令参数
     */
    public static void rsync(String cmds) {
        //RunCMD cmd = new RunCMD("./assets/rsync/rsync " + cmds);
        RunCMD cmd = new RunCMD("rsync " + cmds);
        cmd.printResult();
        cmd.printErrResult();
    }

    /**
     * 搜索进程
     * 
     * @param processName
     *            进程名
     * @return PID
     */
    public static int searchProcess(String processName) {
        String cmd[] = {
                "/bin/bash",
                "-c",
                "ps ux | grep --color=auto " + processName
                        + "  | grep -v grep | cut -d ' ' -f 2" };
        RunCMD sh = new RunCMD(cmd);
        ArrayList<String> resultList = sh.getResult();
        if (resultList.isEmpty()) {
            return -1;
        } else
            return Integer.parseInt(resultList.get(0));
    }
}
